import { getBaseUrl } from '@/service/config'
import { HttpConfig } from '@/service/type'
import { refreshTokenApi } from '@/service/login'
import { loginStore } from '@/store/login'
import { formatUrlParam } from '@/assets/js/utils'
import { jm } from '@/assets/js/aes'

class http {
    get = (config: HttpConfig) => {
        return request({ ...config, method: 'GET' })
    }

    post = (config: HttpConfig) => {
        return request({ ...config, method: 'POST' })
    }

    put = (config: HttpConfig) => {
        return request({ ...config, method: 'PUT' })
    }

    delete = (config: HttpConfig) => {
        return request({ ...config, method: 'DELETE' })
    }
}

const key = '$dJrk23jr32k@123'
const iv = '#$#214324@1u4ivd'
//是否AES加密
const isAes = true

//网络请求封装
const request = (config: HttpConfig) => {
    return awaitWrap(
        new Promise((resolve, reject) => {
            if (!config.header) {
                config.header = { 'Content-Type': 'application/json' }
            }
            //携带token
            let token = uni.getStorageSync('uniToken')
            config.header['Authorization'] = 'bearer ' + token
            // console.log(JSON.stringify(config))
            if (isAes) {
                // //加密
                let e = jm.encrypt(JSON.stringify(config.data), key, iv)
                config.data = {
                    data: e
                }
            }
            uni.request({
                url: getBaseUrl() + config.url,
                method: config.method,
                header: config.header,
                data: config.data,
                success: async (res: any) => {
                    if (isAes) {
                        //解密
                        res.data = JSON.parse(jm.decrypt(res.data.data, key, iv))
                    }
                    let interceptors = config.interceptors != undefined ? config.interceptors : true
                    let fullResult = config.fullResult != undefined ? config.fullResult : false
                    // console.log(JSON.stringify(res));
                    if (res.data.result != 0 && interceptors) {
                        uni.showToast({ title: `${res.data.resultNote}`, icon: 'none' })
                        reject(res.data)
                        if (res.data.result == 5008 || res.data.result == 5009) {
                            logout()
                        }
                    } else {
                        if (fullResult) {
                            resolve(res.data)
                        } else {
                            resolve(res.data.detail)
                        }
                    }
                },
                fail: (err) => {
                    reject(err)
                }
            })
        })
    )
}

const awaitWrap = (promise: any) => {
    return promise
        .then((data: any) => {
            return [data, null]
        })
        .catch((err: any) => {
            return [null, err]
        })
}

const logout = () => {
    loginStore().logout()
    uni.reLaunch({
        url: '/pages/login/login'
    })
}

export default new http()
